package com.yzk.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzk.train.common.constant.TrainConstant;
import com.yzk.train.common.context.MemberLoginContext;
import com.yzk.train.common.exception.BusinessException;
import com.yzk.train.common.exception.ExceptionEnum;
import com.yzk.train.common.response.PageResp;
import com.yzk.train.common.utils.SnowUtil;
import com.yzk.train.member.entity.Passenger;
import com.yzk.train.member.entity.PassengerExample;
import com.yzk.train.member.mapper.PassengerMapper;
import com.yzk.train.member.request.PassengerQueryReq;
import com.yzk.train.member.request.PassengerSaveReq;
import com.yzk.train.member.response.PassengerQueryResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PassengerService {

    @Resource
    private PassengerMapper passengerMapper;

    /**
     * 保存 / 更新 乘车人信息
     * 根据ID来判断 执行操作
     * @param req
     */
    public String save(PassengerSaveReq req) {
        Passenger passenger = BeanUtil.copyProperties(req, Passenger.class);
        DateTime now = DateTime.now();
        passenger.setUpdateTime(now);
        if (ObjectUtil.isNull(passenger.getId())) {
            // 查询我的乘客，如果结果大于10，抛出异常，乘客过多
            List<PassengerQueryResp> minePassengers = queryMine();
            if (minePassengers.size() >= TrainConstant.PASSENGER_SAVE_MAXIMUM)
                throw new BusinessException(ExceptionEnum.PASSENGER_MUCH_ERROR);
            passenger.setId(SnowUtil.getSnowflakeNextId());
            passenger.setMemberId(MemberLoginContext.getId());
            passenger.setCreateTime(now);
            passengerMapper.insert(passenger);
            return "保存成功";
        } else {
            passengerMapper.updateByPrimaryKeySelective(passenger);
            return "编辑成功";
        }
    }

    /**
     * 查询当前会员下的乘车人，如果没有ID
     * 则查询所以会员下的乘车人
     * @param req
     */
    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq req) {
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.setOrderByClause("id desc");
        if (ObjectUtil.isNotNull(req.getMemberId())) {
            passengerExample.createCriteria().andMemberIdEqualTo(req.getMemberId());
        }
        // 使用分页插件
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<Passenger> passengers = passengerMapper.selectByExample(passengerExample);
        // 使用 PageInfo 包装查询结果
        PageInfo<Passenger> pageInfo = new PageInfo<>(passengers);
        // 转换为 PageResp
        List<PassengerQueryResp> list = BeanUtil.copyToList(passengers, PassengerQueryResp.class);
        return new PageResp<>(pageInfo.getTotal(), list);
    }

    /**
     * 根据ID删除乘车人
     * @param id
     */
    public void delete(Long id) {
        passengerMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询我的所有乘客
     */
    public List<PassengerQueryResp> queryMine() {
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.setOrderByClause("name asc");
        passengerExample.createCriteria().andMemberIdEqualTo(MemberLoginContext.getId());
        List<Passenger> list = passengerMapper.selectByExample(passengerExample);
        return BeanUtil.copyToList(list, PassengerQueryResp.class);
    }

}
